/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modele;

/**
 *
 * @author Gloire
 */
public class Grille {
    
    private Case[][] tab;
    private static final double[] frequence = { 8.122, 0.901, 3.345, 3.669,
			17.115, 1.066, 0.866, 0.737, 7.58, 0.545, 0.049, 5.456, 2.968,
			7.095, 5.378, 3.021, 1.362, 6.553, 7.948, 7.244, 6.369, 1.628,
			0.114, 0.387, 0.308, 0.136 };
    
    private static final int[] points = {1,3,3,2,
                        1,4,2,4,1,8,10,1,2,
                        1,1,3,8,1,1,1,1,4,
                        10,10,10,10};
    
    public static double[] getFrequence(double[] tmp) {
		double[] tab = new double[26];
		tab[0] = tmp[0];
		for (int i = 1; i < tab.length; i++)
			tab[i] += tmp[i]+tab[i-1];
		return tab;
	}
    
    public Grille(){
        this.tab = new Case[4][4];
        double r;
        for(int i=0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                r = Math.random()*100;
                for(int k = 0; k < 26; k++){
                    if(r < getFrequence(frequence)[k]){
                        tab[i][j]= new Case((char) (k + 97),points[k]);
                        break;
                    }
                }
            }
        }
        defVoisins();
    }
    
    public Case[][] getGrille(){
        return this.tab;
    }
    
    public Case getCase(int i, int j){
        return tab[i][j];
    }
    
    public void defVoisins(){
        for(int i = 0 ; i < tab.length ; i++){
            for(int j = 0; j < tab[0].length ; j++){
                /*Si elle n'est pas sur la premiere ligne
                 * On rajoute les voisins du haut
                 */
                //haut gauche
                if( i != 0 && j != 0){
                    tab[i][j].getVoisins().add(tab[i-1][j-1]);
                }
                //haut
                if( i != 0){
                    tab[i][j].getVoisins().add(tab[i-1][j]);
                }
                //haut droite
                if( i != 0 && j != tab.length-1){
                    tab[i][j].getVoisins().add(tab[i-1][j+1]);
                }
                //droite
                if(j != tab.length-1){
                    tab[i][j].getVoisins().add(tab[i][j+1]);
                }
                //bas droite
                if( i != tab[0].length-1 && j != tab.length-1){
                    tab[i][j].getVoisins().add(tab[i+1][j+1]);
                }
                //bas
                if( i != tab[0].length-1){
                    tab[i][j].getVoisins().add(tab[i+1][j]);
                }
                //bas gauche
                if( i != tab[0].length-1 && j != 0){
                    tab[i][j].getVoisins().add(tab[i+1][j-1]);
                }
                //gauche
                if(j != 0){
                    tab[i][j].getVoisins().add(tab[i][j-1]);
                }
            }
        }
    }
    
    @Override
    public String toString(){
        String str = "";
        for(int i = 0; i < tab.length; i++){
            for(int j = 0; j < tab.length; j++){
                str += tab[i][j].toString();
            }
            str += "\n";
        }
        return str;
    }
    
}
